package com.ant.storage.service.impl;

import com.ant.storage.common.ResultCode;
import com.ant.storage.dto.PersonnelDto;
import com.ant.storage.handler.AntException;
import com.ant.storage.mapper.PersonnelMapper;
import com.ant.storage.pojo.Personnel;
import com.ant.storage.service.IDataMsgService;
import com.ant.storage.service.IPersonnelService;
import com.ant.storage.utils.DingDingUtil;
import com.ant.storage.vo.PersonnelVo;
import com.ant.storage.vo.PurchaseVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiV2DepartmentListparentbyuserRequest;
import com.dingtalk.api.response.OapiV2DepartmentListparentbyuserResponse;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taobao.api.ApiException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 人事管理 服务实现类
 * </p>
 *
 * @author hzd
 * @since 2023-03-20
 */
@Service
//@DS("threes")
public class PersonnelServiceImpl extends ServiceImpl<PersonnelMapper, Personnel> implements IPersonnelService {

    @Autowired
    private IDataMsgService changeDb;


    @Override
    public PageInfo<PersonnelVo> getPersonnelList(PersonnelDto dto) {
        try {
            changeDb.changeDb(1002);
            PageHelper.startPage(dto.getPageIndex(), dto.getPageSize());
            List<PersonnelVo> list = this.baseMapper.selectPersonnel(dto.getName());
            PageInfo<PersonnelVo> pageInfo = new PageInfo<>(list);
            return pageInfo;
        } catch (Exception e) {
            throw new AntException(ResultCode.BODY_NOT_MATCH);
        }
    }

    @Override
    public Personnel findNailReviewById(String id) {
        return this.baseMapper.findNailReviewById(id);
    }

    @Override
    public Long getPersonneDeplList(String userId, String accessToken) throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk" +
                ".com/topapi/v2/department/listparentbyuser");
        OapiV2DepartmentListparentbyuserRequest req = new OapiV2DepartmentListparentbyuserRequest();
        req.setUserid(userId);
        OapiV2DepartmentListparentbyuserResponse rsp = client.execute(req, accessToken);
        if (!rsp.getErrcode().equals(0L)) {
            throw new AntException("获取钉钉信息失败" + rsp.getErrmsg());
        }
        return rsp.getResult().getParentList().get(0).getParentDeptIdList().get(0);
    }

    @Override
    public PageInfo<PurchaseVo> findNailReview(PersonnelDto dto) {
        try {
            PageHelper.startPage(dto.getPageIndex(), dto.getPageSize());
            List<PurchaseVo> list = this.baseMapper.findNailReview(dto.getName());
            PageInfo<PurchaseVo> pageInfo = new PageInfo<>(list);
            return pageInfo;
        } catch (Exception e) {
            throw new AntException(ResultCode.BODY_NOT_MATCH);
        }
    }

    @Override
    public String[] getAccessToken(PersonnelVo vo) throws AntException, ApiException {
        String dingTalk = vo.getDingTalk();
        String accessToken = "";
        if (StringUtils.isBlank(accessToken)) {
            accessToken = DingDingUtil.obtainToken();
            //stringRedisTemplate.opsForValue().set("accessToken", accessToken, 3500, TimeUnit.SECONDS);
        }
        String[] data = {accessToken, dingTalk};
        return data;
    }

}
